package wwk.common.widget;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ListView;

enum PullState {
	STATUS_NORMAL,
	STATUS_PULL_TO_REFRESH, 
	STATUS_RELEASE_TO_REFRESH,
	STATUS_REFRESHING
	}

public class PullRefreshController implements OnTouchListener {

	private View headerView;
	private ListView listView;
	private ViewGroup.MarginLayoutParams headerViewMarginLayoutParams;

	private int headerViewHeight=0;
	private PullState pullState = PullState.STATUS_NORMAL;
	public PullRefreshInterface pullRefreshInterface;
	private float yDown;

	public PullRefreshController(ListView listView,View headerView)
	{
		this.listView=listView;
		this.headerView=headerView;

		listView.setOnTouchListener(this);

		headerViewMarginLayoutParams=(MarginLayoutParams) headerView.getLayoutParams();
		headerViewHeight=headerViewMarginLayoutParams.height;
		headerViewMarginLayoutParams.setMargins(0, -headerViewHeight, 0, 0);
	}
	
	public PullState pullState()
	{
		return pullState;
	}
	
	private boolean ableToPull()
	{
		boolean ableToPull=true;
		
		View firstChild = listView.getChildAt(0);
		if (firstChild != null) 
		{
			int firstVisiblePos = listView.getFirstVisiblePosition();
			if (firstVisiblePos == 0 && firstChild.getTop() == 0) 
			{
				// ����׸�Ԫ�ص��ϱ�Ե�����븸����ֵΪ0����˵��ListView���������������ʱӦ����������ˢ��
				ableToPull = true;
			} 
			else
			{
				ableToPull = false;
			}
		}

		return ableToPull;
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) 
	{
		boolean consumed=false;
		
		if(!ableToPull())
		{
			return consumed;
		}
		
		int edgeFlags=event.getEdgeFlags();
		if (event.getAction() == MotionEvent.ACTION_DOWN && edgeFlags != 0) 
		{
			return consumed;
		}

		int action=event.getAction();
		
		Log.d("action",""+action);
		
		switch (action) 
		{
			case MotionEvent.ACTION_DOWN:
				{
					yDown = event.getRawY();
					
					consumed=true;//question: �������ʱ��true����ʹ�»�ʱ�����ֽ��㡣����ʱ���Ҳû���˽���...
				}
				break;
			case MotionEvent.ACTION_MOVE:
				{
					if (pullState != PullState.STATUS_REFRESHING) 
					{
						int distance = (int) (event.getRawY() - yDown);

						if (distance <= 0 
								&& headerViewMarginLayoutParams.topMargin <= -headerViewHeight) 
						{
							resetPullState();
						}
						else
						{
							final int antiskidHeight=0;
							
							if (headerViewMarginLayoutParams.topMargin >= 0) 
							{
								pullState = PullState.STATUS_RELEASE_TO_REFRESH;
								
								consumed=true;
								
								headerViewMarginLayoutParams.topMargin = -headerViewHeight+distance ;
								headerView.setLayoutParams(headerViewMarginLayoutParams);
							} 
							else if (headerViewMarginLayoutParams.topMargin >= -headerViewHeight+antiskidHeight) 
							{
								pullState = PullState.STATUS_PULL_TO_REFRESH;
								
								consumed=true;
								
								headerViewMarginLayoutParams.topMargin = -headerViewHeight+distance ;
								headerView.setLayoutParams(headerViewMarginLayoutParams);
							}
							else
							{
								pullState = PullState.STATUS_NORMAL;
							}
						}
					}
				}
				break;
			case MotionEvent.ACTION_UP:
				{
					switch(pullState)
					{
						case STATUS_RELEASE_TO_REFRESH:
							{
								headerViewMarginLayoutParams.topMargin = 0;
								headerView.setLayoutParams(headerViewMarginLayoutParams);

								pullState = PullState.STATUS_REFRESHING;
								
								// ����ʱ������ͷ�����ˢ��״̬����ȥ��������ˢ�µ�����
								//new RefreshingTask().execute();
								if(pullRefreshInterface!=null)
								{
									pullRefreshInterface.onPullRefreshing();
								}	
								
								consumed=true;
							} 
							break;
						case STATUS_PULL_TO_REFRESH:
							{
								// ����ʱ���������״̬����ȥ������������ͷ������
								//new HideHeaderTask().execute();
								
								resetPullState();
								
								consumed=true;
							}
							break;
						default:
							{
								
							}
							break;
					}
				}
				break;
			case MotionEvent.ACTION_CANCEL:
				{
					Log.d("MotionEvent","ACTION_CANCEL");
				}
				break;
			default:
				{
				
				}
				break;
		}//end switch
		
		return consumed;
	}
	
	public final boolean onInterceptTouchEvent(MotionEvent event)
	{
		return true;
	}
	
	public void resetPullState() 
	{
		pullState=PullState.STATUS_NORMAL;
		
		headerViewMarginLayoutParams.topMargin = -headerViewHeight;
		headerView.setLayoutParams(headerViewMarginLayoutParams);
	}
}
